// startDate: 计划开始时间； endDate：计划结束时间；dayLength：每隔几天，0-代表每天，1-代表日期间隔一天
export function getDateGap (startDate, endDate, dayLength = 0) {
  let str = startDate;
  for (let i = 0; ; i++) {
    const getDate = getTargetDate(startDate, dayLength);
    startDate = getDate;
    if (getDate <= endDate) {
      str += ',' + getDate;
    } else {
      break;
    }
  }
  return str.split(',')
}

// startDate: 开始时间；dayLength：每隔几天，0-代表获取每天，1-代表日期间隔一天
export function getTargetDate (date, dayLength) {
  dayLength = dayLength + 1;
  const tempDate = new Date(date);
  tempDate.setDate(tempDate.getDate() + dayLength);
  const year = tempDate.getFullYear();
  const month = tempDate.getMonth() + 1 < 10 ? '0' + (tempDate.getMonth() + 1) : tempDate.getMonth() + 1;
  const day = tempDate.getDate() < 10 ? '0' + tempDate.getDate() : tempDate.getDate();
  return year + '-' + month + '-' + day;
}

// 获取当前日期 YYYY-mm-dd
export function getNowFormatDate () {
  const date = new Date();
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = '0' + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate;
  }
  const currentdate = year + '-' + month + '-' + strDate;
  return currentdate;
}

// 获取本周周一 参数为当前日期
export function getCurrentWeekFirstDay (date = new Date()) {
  let days = date.getDay();
  days = days === 0 ? 7 : days;
  const weekFirstDay = new Date(date - (days - 1) * 86400000);
  let firstMonth = Number(weekFirstDay.getMonth()) + 1;
  if (firstMonth < 10) {
    firstMonth = '0' + firstMonth;
  }
  let weekFirstDays = weekFirstDay.getDate();
  if (weekFirstDays < 10) {
    weekFirstDays = '0' + weekFirstDays;
  }
  return weekFirstDay.getFullYear() + '-' + firstMonth + '-' + weekFirstDays;
}

// 获取当月第一天
export function getCurrentMonthFirstDay () {
  const first = new Date().setDate(1)
  return formatDate(new Date(first))
}

// 日期格式化
export function formatDate (date = new Date()) {
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = '0' + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate;
  }
  const currentdate = year + '-' + month + '-' + strDate;
  return currentdate;
}

// 获取上个月第一天
export function lastMonthFirst () {
  const time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth();
  const day = time.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (month === 0) {
    year = year - 1;
    month = 12;
  }
  // 上个月的第一天
  const v = year + '-' + month + '-' + '0' + 1;
  return v;
}

// 获取上个月最后一天
export function lastMonthLast () {
  const time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth();
  const day = time.getDate();
  if (month < 10) {
    month = '0' + month;
  }
  if (month === 0) {
    year = year - 1;
    month = 12;
  }

  const lastday = new Date(year, month, 0).getDate();
  // 上个月的最后一天
  const v2 = year + '-' + month + '-' + lastday;
  return v2;
}

// 获取这个月的第一天
export function MonthFirst () {
  const time = new Date();
  const year = time.getFullYear();
  let month = time.getMonth() + 1;
  if (month < 10) {
    month = '0' + month;
  }
  const v = year + '-' + month + '-' + '0' + 1;
  return v;
}

// 获得上周周一~周日的年月日
export function getLastWeekData () {
  const lastweek = {};
  const date = new Date();
  // 上周一的日期
  date.setDate(date.getDate() - 7 - date.getDay() + 1);
  const startDay = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

  // 上周日的日期
  date.setDate(date.getDate() + 6);
  const endDay = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  lastweek.startDay = formatDate(new Date(startDay))
  lastweek.endDay = formatDate(new Date(endDay))
  return lastweek
}

// 获取过去x天的日期
export function getLastDate (x, date = new Date()) {
  const newDate = new Date(date.getTime() - x * 24 * 60 * 60 * 1000);
  return formatDate(newDate)
}

// 获取两个日期间的月份
export function getMonthBetween (start, end) {
  const result = [];
  const s = start.split('-');
  const e = end.split('-');
  const min = new Date();
  const max = new Date();
  min.setFullYear(s[0], s[1]);
  max.setFullYear(e[0], e[1]);
  const curr = min;
  let str = '';
  let currTimestamp = curr.getTime()
  const maxTimestamp = max.getTime()
  while (currTimestamp <= maxTimestamp) {
    const month = curr.getMonth();
    if (month === 0) {
      str = (curr.getFullYear() - 1) + '-' + 12;
    } else {
      str = curr.getFullYear() + '-' + (month < 10 ? ('0' + month) : month);
    }
    result.push(str);
    curr.setMonth(month + 1);
    currTimestamp = curr.getTime()
  }
  return result;
}
